<template>
	<view class="content">
		<view class="layer" v-show="!isLogin">
			<image class="img" src="../../static/img/company.jpg" mode=""></image>
			<text class="hellow">
				欢迎您进入龙兴矿业集团员工智能测评系统，本系统仅提供给本集团内部员工进行测评.系统检测您还没有登录，登录后才能显示测评列表。
			</text>
			<button class="btn" @click="wechatSignin">点击此处登录</button>
		</view>
		<view class="layer" v-show="isLogin">
			<text class="nothing" v-if="exam_list.length == 0">没有任何测评</text>
			<view class="testing-box" v-for="(exam,idx) in exam_list" :key="exam.examId" @click="toTesting(exam.examId)" v-if="exam_list.length > 0">
				<text v-text="exam.examName"></text>
			</view>
		</view>
	</view>
</template>

<script>
	import logic from '../../common/logic.js';
	export default {
		data() {
			return {
				singup:false,
				isLogin:false,
				exam_list:[]
			}
		},
		onLoad() {
			this.isLogin = logic.global.isLogin;
			console.log('index onload');
		},
		onShow(){
			//如果登录则获取测评列表
			if(this.isLogin){
				this.getTestList();
			}
		},
		onShareAppMessage(){
			return {
				path:'/pages/index/index',
				title:'龙兴矿业集团智能测评系统'
			}
		},
		methods: {
			//获取所有的正在进行的测评列表。
			getTestList(){
				console.log('getTestList')
				let that = this;
				uni.showLoading({
					title: '正在获取数据，请稍后...'
				});
				uni.request({
					url:String(logic.urls.getTestList),
					data: {},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						console.log(res);
						let rs = res.data;
						if(rs.status == 1){
							that.exam_list = rs.data;
							console.log(that);
						}else{
							uni.showToast({
								title:'获取测评列表失败，代码为' + rs.msg,
								icon:'none',
								duration:3000
							});
						}
					},
					fail: (res) => {
						console.log(res);
						uni.showToast({
							title:'连接服务器失败，请稍后再试',
							icon:'none',
							duration:3000
						});
					},
					complete: () => {
						uni.hideLoading();
					}
				})
			},
			toTesting(examId){
				console.log(examId);
				if(examId <= 0){
					uni.showToast({
						title:'测评ID错误',
						icon:'none',
						duration:3000
					});
				}
				let url = `../../pages/testing/testing?examId=${examId}`;
				console.log(url);
				uni.navigateTo({
					url:url
				});
			},
			//跳转到登录页面
			wechatSignin(){
				uni.redirectTo({
					url:'../../pages/login/login'
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height:100%;
	}
	.layer{
		width:100%;
		display:flex;
		flex-flow:column;
		justify-content: space-around;
		align-items: center;
	}
	.testing-box{
		width:90%;
		height:260rpx;
		background:linear-gradient(33deg, #54E38E, #41C7AF);
		color:#fff;
		display:flex;
		justify-content: center;
		align-items: center;
		border-radius:20rpx;
		font-size:40rpx;
		margin-top:40rpx;
	}
	.nothing{
		width:90%;
		height:240rpx;
		color:#000;
		background-color: #dedede;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 20rpx;
		font-size: 40rpx;
		margin-top:40rpx;
	}
	.img{
		width:750rpx;
		height:422rpx;
	}
	.hellow{
		width:100%;
		height:330rpx;
		background:linear-gradient(33deg, #54E38E, #41C7AF);
		color:#fff;
		font-size:40rpx;
		text-align:center;
		line-height: 80rpx;
		box-sizing: border-box;
		padding:0 20rpx;
	}
	.btn{
	  width:80%;
	  height:100rpx;
	  background:#0CB9D5;
	  color:#fff;
	  font-size:40rpx;
	  text-align:center;
	  margin:80rpx auto;
	  border-radius:50rpx;
	  overflow:hidden;
	}
</style>
